package service.pqueue;

import java.net.InetSocketAddress;
import java.nio.charset.Charset;

import org.apache.mina.core.session.IdleStatus;
import org.apache.mina.filter.codec.ProtocolCodecFilter;
import org.apache.mina.filter.logging.LoggingFilter;
import org.apache.mina.transport.socket.nio.NioSocketAcceptor;

import funcs.mina.codec.TextLineCodecFactory;

public class QServer {
	public static final String STORE_TYPE_QUEUE = "1";
	public static final String STORE_TYPE_HASHQUEUE = "2";

	public static IPQueue queue;

	public static NioSocketAcceptor acceptor = new NioSocketAcceptor();

	public static void main(String[] args) throws Throwable {

		if (STORE_TYPE_HASHQUEUE.equals(QServerContext.SERVER_TYPE)) {
			System.out.println("USE:PHashQueue");
			queue = new PHashQueue();
		} else {
			System.out.println("USE:PQueue");
			queue = new PQueue();
		}

		acceptor.getFilterChain().addLast(
				"codec",
				new ProtocolCodecFilter(new TextLineCodecFactory(Charset
						.forName("UTF-8"))));
		acceptor.getFilterChain().addLast("logger", new LoggingFilter());
		acceptor.setHandler(new ServerSessionHandler());
		acceptor.getSessionConfig().setReadBufferSize(2048);
		acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 10);

		acceptor.bind(new InetSocketAddress(QServerContext.SERVER_PORT));

		System.out.println("Listening on port " + QServerContext.SERVER_PORT);
	}
}
